package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.admin.req.user.CapPledgeMiningIncomeRecordPageReq;
import com.letoken.platform.repo.admin.req.user.FundDetailReq;
import com.letoken.platform.repo.app.po.AppFundDetail;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2023-08-04
 */
public interface AppFundDetailMapper extends BaseMapper<AppFundDetail> {
    @Select("select IFNULL(sum(amount),0) from app_user_assets_history where type in (20,21,22,27) and to_days(now( ) ) - to_days( create_time) = 1 ")
    BigDecimal yesterdayIncome();

    @Select("select IFNULL(sum(amount),0) from app_user_assets_history where type in (20,21,22,27) and DATE(create_time) = CURDATE()")
    BigDecimal todayIncome();

    @Select("select IFNULL(sum(amount),0) from app_user_assets_history where type in (20,21,22,27) and MONTH(create_time) = MONTH(NOW()) and create_time >= '2023-10-13 2:00:00'")
    BigDecimal mouthIncome();

    @Select("select IFNULL(sum(amount),0) from app_user_assets_history where type in (20,21,22,27) and create_time >= '2023-10-13 2:00:00' ")
    BigDecimal sumIncome();

    Page<AppFundDetail> getFundDetail(@Param("page") Page<AppFundDetail> page, @Param("req") FundDetailReq req);

    @Select({"<script>",
            " select ifnull(SUM(amount),0) " +
                    " from app_fund_detail " +
                    "where type in (1,2)" +
                    "   <if test='null != startTime'> " +
                    "      and create_time &gt;= #{startTime}" +
                    "   </if>" +
                    "   <if test='null != endTime'> " +
                    "     and  create_time &lt;= #{endTime}" +
                    "   </if>" +
                    "</script>"})
    BigDecimal sum(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);


    @Select({"<script>",
            " select user_id, amount, create_time , type " +
            " from app_fund_detail" +
            "<where>" +
            "   <choose>" +
            "       <when test='null != req.type'> " +
            "           type = #{req.type}" +
            "       </when>" +
            "       <otherwise> " +
            "           type in (1,2)" +
            "       </otherwise>" +
            "   </choose>" +
            "   <if test='null != req.userId'> " +
            "     and  user_id = #{req.userId}" +
            "   </if>" +
            "   <if test='null != req.startTime'> " +
            "     and  create_time &gt;= #{req.startTime}" +
            "   </if>" +
            "   <if test='null != req.endTime'> " +
            "    and   create_time &lt;= #{req.endTime}" +
            "   </if>" +
            "</where>" +
            "</script>"})
    Page<AppFundDetail> page(@Param("page") Page page, @Param("req") CapPledgeMiningIncomeRecordPageReq req);
}
